#pragma once
#include <string.h>
#include <dinput.h>
#include <windows.h>

// Define movement flags
typedef enum
{
	KEY_LEFT = 1,
	KEY_RIGHT = 2,
	KEY_UP = 4,
	KEY_DOWN = 8,
	KEY_SPACE = 16,
	KEY_ENTER = 32,
	KEY_A = 64,
	KEY_B = 128,
	KEY_C = 256,
	KEY_D = 512,
	KEY_E = 1024,
	KEY_F = 2048,
	KEY_G = 4096,
	KEY_H = 8192,
	KEY_I = 16384,
	KEY_J = 32768,
	KEY_K = 65536,
	KEY_L = 131072,
	KEY_M = 262144,
	KEY_N = 524288,
	KEY_O = 1048576,
	KEY_P = 2097152,
	KEY_Q = 4194304,
	KEY_R = 8388608,
	KEY_S = 16777216,
	KEY_T = 33554432,
	KEY_U = 67108864,
	KEY_V = 134217728,
	KEY_W = 268435456,
	KEY_X = 536870912,
	KEY_Y = 1073741824,
	KEY_Z = 2147483648,
	
}key_t;

class Keyboard
{
private:
	LPDIRECTINPUT8        g_lpDI;	// the direct input object
	LPDIRECTINPUTDEVICE8  g_lpDIDevice; // the direct input device

	unsigned long keys;
	unsigned long lastKeys;
	char buffer[256];
public:

	Keyboard::Keyboard(HINSTANCE hInstance, HWND wndHandle)
	{	
		// Create the DirectInput object. 
		DirectInput8Create(hInstance, DIRECTINPUT_VERSION, 
								IID_IDirectInput8, (void**)&g_lpDI, NULL); 

		
		// Retrieve a pointer to an IDirectInputDevice8 interface 
		g_lpDI->CreateDevice(GUID_SysKeyboard, &g_lpDIDevice, NULL); 

		g_lpDIDevice->SetDataFormat(&c_dfDIKeyboard); 

		 

		// Set the cooperative level 
		g_lpDIDevice->SetCooperativeLevel(wndHandle, 
								 DISCL_FOREGROUND | DISCL_NONEXCLUSIVE); 

		// Get access to the input device. 
		g_lpDIDevice->Acquire(); 
		
		keys = lastKeys = 0;

		for (int i = 0; i < 256; i++)
			buffer[i] = 0;

	}

	Keyboard::~Keyboard()
	{
		if (g_lpDI) 
		{ 
			if (g_lpDIDevice) 
			{ 
			// Always unacquire device before calling Release(). 
				g_lpDIDevice->Unacquire(); 
				g_lpDIDevice->Release();
				g_lpDIDevice = NULL; 
			} 
			g_lpDI->Release();
			g_lpDI = NULL; 
		}
	}

	void pingKeyboard();

	bool isKeyHeld(key_t keyCheck); // Key is being held
	bool isKeyUp(key_t keyCheck); // Check if key is unpressed
	bool isKeyPressed(key_t keyCheck); // Key was pressed recently
	bool isKeyReleased(key_t keyCheck); // Key was released recently
};